package com.hengxinguotong.zhihuichengjian.weekpicker.entity;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class WeekHelper {

    /**
     * 获取所有年份
     * @return List<Integer>
     */
    public static List<Integer> getYears() {
        List<Integer> yearList = new ArrayList<>();
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        // 添加上下100年
        for(int i = (currentYear - 100);i <= (currentYear + 100) ; i++){
            yearList.add(i);
        }
        return yearList;
    }

    /**
     * 获取所有月份
     * @return List<Integer>
     */
    public static List<Integer> getMonths() {
        List<Integer> monthList = new ArrayList<>();
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        // 添加上下100年
        for(int i = 0;i < 12; i++){
            monthList.add(i);
        }
        return monthList;
    }

    /**
     * 获取当月内所有周
     * @param year
     * @param month
     * @return List<Week>
     */
    public static List<Week> getWeeksByYearAndMonth(int year, int month){
        List<Week> weekList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        Week week = new Week();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            calendar.add(Calendar.DAY_OF_MONTH, -6);
        }
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        Date weekBegin = calendar.getTime();
        week.setWeekBegin(weekBegin);
        calendar.add(Calendar.DAY_OF_MONTH, 6);
        Date weekEnd = calendar.getTime();
        week.setWeekEnd(weekEnd);
        week.setWeekNum(1);
        weekList.add(week);
        //一月最多7周
        for (int i = 0; i < 6; i++) {
            week = new Week();
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            int curMonth = calendar.get(Calendar.MONTH);
            weekBegin = calendar.getTime();
            week.setWeekBegin(weekBegin);
            calendar.add(Calendar.DAY_OF_MONTH, 6);
            weekEnd = calendar.getTime();
            week.setWeekEnd(weekEnd);
            week.setWeekNum(i + 2);
            if(curMonth != month){
                break;
            }else {
                weekList.add(week);
            }
        }
        return weekList;
    }

    /**
     * 获取当前周下标
     * @param weekList
     * @return int
     */
    public static int getSelectWeekIndex(List<Week> weekList){
        for (int i = 0; i < weekList.size(); i++) {
            Week week = weekList.get(i);
            Date today = new Date();
            if(today.getTime() < week.getWeekEnd().getTime() && today.getTime() > week.getWeekBegin().getTime()){
                return i;
            }
        }
        return 0;
    }

    /**
     * 获取当前周信息
     * @param weekList
     * @return Week
     */
    public static Week getSelectWeek(List<Week> weekList){
        for (int i = 0; i < weekList.size(); i++) {
            Week week = weekList.get(i);
            Date today = new Date();
            if(today.getTime() < week.getWeekEnd().getTime() && today.getTime() > week.getWeekBegin().getTime()){
                return week;
            }
        }
        return null;
    }

}